/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.liquidByte.trafficMustang.server.business;

import java.math.BigDecimal;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.liquidByte.trafficMustang.server.dataModel.Localizacao;
import org.liquidByte.trafficMustang.server.dataModel.Rastreamento;
import org.liquidByte.trafficMustang.server.dataModel.RastreamentoPK;
import org.liquidByte.trafficMustang.server.dataModel.Usuario;
import org.liquidByte.trafficMustang.server.repository.RastreamentoRepository;
import org.liquidByte.trafficMustang.server.services.LoginService;
import org.liquidByte.trafficMustang.server.services.RastreamentoService;
import org.liquidByte.trafficMustang.server.services.UsuarioService;

/**
 *
 * @author Alexandre
 */
@Stateless
public class RastreamentoBO implements RastreamentoService {
    
    @EJB
    private RastreamentoRepository repository;
    @EJB
    private UsuarioService usuarioService;
    @EJB
    private LoginService login;

    public void update(double longitude, double latitude) {
        Usuario usr = this.login.getUsuario();
        if (usr != null) {
            Rastreamento rastreamento = new Rastreamento();

            RastreamentoPK id = new RastreamentoPK();
            id.setUsrId(usr.getUsrId());
            id.setRastrDt(new Date());

            Localizacao localizacao = new Localizacao(new BigDecimal(longitude), new BigDecimal(latitude));

            rastreamento.setRastreamentoPK(id);
            rastreamento.setUsuario(usr);
            rastreamento.setLocalizacao(localizacao);

            this.repository.persist(rastreamento);
        }
    }

    public Localizacao getLocation(Usuario usuario) {
        Localizacao localizacao = null;
        if (usuario != null) {
            localizacao = this.repository.getLastLocation(usuario);
        }
        return localizacao;
    }
}
